import home from "../pages/home";

export default class sceneutil {
    public realHeigt = 0;   //屏幕实际高度
    public shejibili = { width: 720, height: 1560 };
    private static _Instance: sceneutil;
    public partsetting: Laya.Particle2D;
    public pool = {};// 对象池
    static get Instance(): sceneutil {
        if (!this._Instance)
            this._Instance = new sceneutil();
        return this._Instance;
    }
    constructor() {
        this.realHeigt = window.innerHeight * this.shejibili.width / window.innerWidth;
        console.log("sceneutil init", this.realHeigt);

    }

    initpart() {
        Laya.loader.load('./star.part', Laya.Handler.create(this, (settings) => {
            this.partsetting = settings;
        }), null, Laya.Loader.JSON)
    }
    //初始化字体
    initFont() {
        var bitmapFont: Laya.BitmapFont = new Laya.BitmapFont();
        bitmapFont.loadFont("font/f.fnt", new Laya.Handler(this, this.onFontLoaded, [bitmapFont]));
    }

    private onFontLoaded(bitmapFont: Laya.BitmapFont): void {
        bitmapFont.setSpaceWidth(10);
        Laya.Text.registerBitmapFont("f", bitmapFont);
        // this.createText(this.fontName);
        // game.Instance.move.font = "f";
        // game.Instance.move.scale(1.4, 1.4);
        // game.Instance.bushu.font = "f"
        // game.Instance.lev.font = "f"
        // game.Instance.levnum.font = "f"

    }

    //打开界面回弹效果
    public sceneOpen(scene: Laya.Scene) {
        scene.scale(0, 0);
        scene.pivot(sceneutil.Instance.shejibili.width / 2, sceneutil.Instance.shejibili.height / 2);
        scene.pos(sceneutil.Instance.shejibili.width / 2, sceneutil.Instance.shejibili.height / 2);
        Laya.Tween.to(scene, { scaleX: 1, scaleY: 1 }, 300, Laya.Ease.backOut);
        return 300;
    }


    public playlizi(x, y) {
        let pa = sceneutil.Instance.partcle() as Laya.Particle2D;
        pa.x = x;
        pa.y = y;
        Laya.stage.addChild(pa)
        pa.play();
        pa.zOrder = 20;
        setTimeout(() => {
            pa.stop();
            setTimeout(() => {
                Laya.stage.removeChild(pa)
            }, 1000);
        }, 500)
    }


    //卡牌晃动
    public cardShake(card: any, time?: number) {
        if (time == 0) {
            return;
        }
        // console.log(time, time == 0)
        let jiaodu = 15;
        let shaketime = 35;//0-jiaodu 用的时间
        Laya.Tween.to(card, { rotation: jiaodu }, shaketime, null, Laya.Handler.create(this, () => {
            Laya.Tween.to(card, { rotation: -jiaodu }, 2 * shaketime, null, Laya.Handler.create(this, () => {
                Laya.Tween.to(card, { rotation: 0 }, shaketime, null, Laya.Handler.create(this, () => {
                    this.cardShake(card, --time)
                }), null, true)
            }), null, true)
        }), null, true)
    }

    //字体内容, y  ,  字体
    public floatText(text: string, y: number, parent: any, font?: string) {
        let txt = Laya.Pool.getItemByClass("floattext", Laya.Text) as Laya.Text;
        txt.fontSize = 50;
        txt.text = text;
        txt.x = this.shejibili.width / 2;
        txt.y = y;
        txt.pivotX = txt.width / 2;
        txt.pivotY = txt.height / 2;
        txt.scale(0, 0);
        txt.alpha = 100;
        if (font) {
            txt.font = font;
        }
        parent.addChild(txt);
        Laya.Tween.to(txt, { scaleX: 1, scaleY: 1 }, 200, Laya.Ease.backOut, Laya.Handler.create(this, () => {
            Laya.Tween.to(txt, { y: txt.y - 50, alpha: 0 }, 550, Laya.Ease.linearNone, Laya.Handler.create(this, () => {
                Laya.stage.removeChild(txt);
                Laya.Pool.recover("floattext", txt);
            }))
        }))
    }


    public fadeout(time: number, node: any, destory?: boolean) {
        Laya.Tween.to(node, { alpha: 0 }, time, null, Laya.Handler.create(this, () => {
            if (destory) {
                node.destroy();
            }
        }));
    }


    public partcle() {
        // Laya.Pool.getItemByClass("partcle", Laya.Particle2D)
        // this.part = new Laya.Particle2D(settings);
        // sp.emitter.start();
        // sp.x = Laya.stage.width / 2;
        // sp.y = Laya.stage.height / 2;
        // this.part.play();
        // Laya.stage.addChild(this.part);
        return this.poolcreateByClass("partcle", Laya.Particle2D, this.partsetting);

    }



    //对象池创建对象方法   创建对象通过传入的 sign 跟类型  clas 为类名
    poolcreateByClass(sign: string, clas: any, param?: any) {
        let arr;
        if (this.pool[sign]) {
            arr = this.pool[sign];
        } else {
            this.pool[sign] = [];
            arr = [];
        }
        if (arr.length == 0) {
            if (param)
                return new clas(param);
            return new clas();
        } else {
            return arr.shift();
        }
    }

    //对象池销毁对象通过传入的sign跟类型  clasinstance 为类的实例  创建时候 必须放到stage上 方便到时候销毁
    poolrecover(sign: string, clasinstance: any) {
        //重制原有对象  塞入数组
        this.pool[sign].push(clasinstance);
        Laya.stage.removeChild(clasinstance);
    }


    showBlackBack() {
        home.Instance.blackback.visible = true;
    }
    hideBlackBack() {
        home.Instance.blackback.visible = false;
    }


}